package test07;

/**
 * 抽象内容处理者
 */
public abstract class ContentHandler {
    protected ContentHandler nextHandler;
    protected String handlerName;

    public ContentHandler(String handlerName) {
        this.handlerName = handlerName;
    }

    public void setNext(ContentHandler nextHandler) {
        this.nextHandler = nextHandler;
    }

    /**
     * 处理审核请求
     */
    public final void handleCheck(ContentCheckRequest request) {
        long startTime = System.currentTimeMillis();

        // 检查是否能处理该类型内容
        if (canHandle(request)) {
            System.out.println("🔍 " + handlerName + " 开始处理...");

            // 执行具体检查
            CheckResult result = doCheck(request);
            long processingTime = System.currentTimeMillis() - startTime;

            // 记录处理历史
            ProcessingRecord record = new ProcessingRecord(
                    handlerName, result.getMessage(), processingTime, result.getConfidence()
            );
            request.addProcessingRecord(record);

            // 更新请求风险等级
            if (result.getRiskLevel().ordinal() > request.getRiskLevel().ordinal()) {
                request.setRiskLevel(result.getRiskLevel());
            }

            System.out.println("   " + result);
            System.out.println("   ⏱️ 处理时间: " + processingTime + "ms");

            // 根据结果决定是否继续传递
            if (result.shouldStopChain()) {
                System.out.println("   ⏹️ 链式处理终止");
                return;
            }
        } else {
            System.out.println("⏭️ " + handlerName + " 跳过处理（不支持该内容类型）");
        }

        // 传递给下一个处理者
        if (nextHandler != null) {
            nextHandler.handleCheck(request);
        } else {
            System.out.println("✅ 所有审核流程完成，最终风险等级: " + request.getRiskLevel());
        }
    }

    /**
     * 具体检查逻辑 - 由子类实现
     */
    protected abstract CheckResult doCheck(ContentCheckRequest request);

    /**
     * 判断是否能处理该类型内容 - 由子类实现
     */
    protected abstract boolean canHandle(ContentCheckRequest request);
}
